/* $Id: ContestProblemsModel.java 137 2011-08-29 20:33:36Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.models;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;

import alt.djudge.frontend.server.Deployment;
import alt.djudge.frontend.server.datatypes.ContestProblemEntry;
import alt.djudge.frontend.server.datatypes.ProblemEntry;


public class ContestProblemsModel
{
	private static CoreInterface<ContestProblemEntry> core = Deployment.isGAE() ?  
		new AbstractBigtableModelCore<ContestProblemEntry>()
		{
			@Override
			protected Order getOrdering()
			{
				return Order.asc("sid");
			}
			
			@Override
			protected Class<ContestProblemEntry> getEntryClass()
			{
				return ContestProblemEntry.class;
			}
		}
	:
		new AbstractDBModelCore<ContestProblemEntry>()
		{
			@Override
			protected Order getOrdering()
			{
				return Order.asc("sid");
			}
			
			@Override
			protected Class<ContestProblemEntry> getEntryClass()
			{
				return ContestProblemEntry.class;
			}
		};		
	
	public static List<ContestProblemEntry> getAllEntries()
	{
		return core.getAllEntries();
	}

	public static List<ContestProblemEntry> getEntries(Map<String, Object> map)
	{
		return core.getEntries(map);
	}

	public static List<ContestProblemEntry> getContestEntries(Long contestId)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("contestId", contestId);
		return core.getEntries(map);
	}

	public static Map<Long, ProblemEntry> getContestEntriesMap(Long contestId)
	{
		HashMap<Long, ProblemEntry> map = new HashMap<Long, ProblemEntry>();
		List<ContestProblemEntry> contestProblemEntries = getContestEntries(contestId);
		Map<Long, ProblemEntry> problemsMap = ProblemsModel.getEntriesMap();
			
		for (ContestProblemEntry contestProblemEntry : contestProblemEntries)
			map.put(contestProblemEntry.getId(), problemsMap.get(contestProblemEntry.getProblemId()));
		return map;
	}
	
	public static ContestProblemEntry getEntry(Long contestProblemId)
	{
		return core.getEntry(contestProblemId);
	}
	
	public static boolean deleteEntry(Long contestProblemId)
	{
		return core.deleteEntry(contestProblemId);
	}
	
	public static HashMap<Long, ContestProblemEntry> getEntriesMap()
	{
		List<ContestProblemEntry> list = getAllEntries();
		HashMap<Long, ContestProblemEntry> result = new HashMap<Long, ContestProblemEntry>();
		for (ContestProblemEntry entry: list)
			result.put(entry.getId(), entry);
		return result;
	}	
	
	public static CoreInterface<ContestProblemEntry> getCore()
	{
		return core.getAdapter();
	}
}
